<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% content_for :page_title, @context.name %>
<% css_bundle :course_show %>
<% @active_tab ||= "home" %>
<h1 class='screenreader-only'><%= @context.name %></h1>
<% content_for :auto_discovery do %>
  <% if @context_enrollment %>
    <%= auto_discovery_link_tag(:atom, feeds_course_format_path(@context_enrollment.feed_code, :atom), {:title => t("Course Atom Feed")}) %>
  <% elsif @context.available? %>
    <%= auto_discovery_link_tag(:atom, feeds_course_format_path(@context.feed_code, :atom), {:title => t("Course Atom Feed")}) %>
  <% end %>
<% end %>

<% content_for :right_side do %>
  <div id="course_show_secondary">
    <% @can_manage = can_do(@context, @current_user, :manage) %>
    <% @can_create_announcements = @context.announcements.temp_record.grants_right?(@current_user, session, :create) %>
    <% if can_do(@context, @current_user, :change_course_state) && (@context.unpublished? || @context.unpublishable?) %>
      <div id="course_status">
        <h3>
          {{#t}}Course Status{{/t}}
        </h3>
        <div id='course_status_actions' class="ui-buttonset">
          <%= form_for @context, html: {id: "course_status_form"} do |f| %>
            <% if @context.created? || @context.claimed? %>
              <button type="button" class='ui-button btn-unpublish disabled' disabled>
                <i class="icon-unpublish"></i>{{#t}}Unpublished{{/t}}
              </button>
              <button type='submit' class="ui-button btn-publish">
                <i class="icon-publish icon-Solid"></i>{{#t}}Publish{{/t}}
              </button>
              <input type='hidden' name='course[event]' value='offer' />
            <% else %>
              <button type='submit' class="ui-button">
                <i class="icon-unpublish"></i>&nbsp;{{#t}}Unpublish{{/t}}
              </button>
              <button type="button" class="ui-button disabled btn-published" disabled>
                <i class="icon-publish icon-Solid"></i>{{#t}}Published{{/t}}
              </button>
              <input type='hidden' name='course[event]' value='claim' />
            <% end %>
          <% end %>
        </div>
      </div>
    <% end %>

    <% if @can_manage || @can_create_announcements || @course_home_view != 'feed' || @course_home_sub_navigation_tools.present? %>
      <div class="course-options">
        <%= external_tools_menu_items(@course_home_sub_navigation_tools, {link_class: "btn button-sidebar-wide course-home-sub-navigation-lti", settings_key: :course_home_sub_navigation}) %>
        <% if @can_manage %>
          <div id="choose_home_page"></div>
          <div id="choose_home_page_not_modules"></div>
        <% end %>

        <% if @course_home_view != 'feed' %>
          <a class="btn button-sidebar-wide" href="<%= context_url(@context, :context_url, :view => 'feed') %>">
            <i class="icon-stats"></i>
            {{#t}}View Course Stream{{/t}}
          </a>
        <% end %>

        <% if @can_manage && !@domain_root_account.try(:feature_enabled?, :new_user_tutorial) %>
          <% set_js_course_wizard_data %>
          <% js_bundle :course_wizard %>
          <% css_bundle :course_wizard %>
          <a href="#" class="btn button-sidebar-wide wizard_popup_link  <%= 'auto_open' if @context.created? || @context.claimed? %>">
            <i class="icon-question"></i> {{#t}}Course Setup Checklist{{/t}}
          </a>
        <% end %>

        <% if @can_create_announcements %>
          <a class="btn button-sidebar-wide" href="<%= context_url(@context, :new_context_discussion_topic_url, :is_announcement => true) %>"><i class="icon-announcement"></i> {{#t}}New Announcement{{/t}}
          </a>
        <% end %>

        <% course_custom_links.each do |link| %>
          <a class="btn button-sidebar-wide" href="<%= link[:url] %>"><i class="<%= link[:icon_class] %>" role="presentation"></i> <%= link[:text] %></a>
        <% end %>
      </div>
    <% end %>

    <% if @context.available? && @context.self_enrollment_enabled? && @context.open_enrollment && (!@context_enrollment || !@context_enrollment.active?) %>
      <a href="<%= enroll_url(@context.self_enrollment_code) %>" class="btn button-sidebar-wide self_enrollment_link" data-open-as-dialog>
        <i class="icon-user-add"></i>
        {{#t}}Join this Course{{/t}}
      </a>
    <% elsif @context_enrollment && @context_enrollment.self_enrolled && @context_enrollment.active? %>
      <a href="#" class="btn button-sidebar-wide self_unenrollment_link">
        <i class="icon-end"></i>
        {{#t}}Drop this Course{{/t}}
      </a>
      <div id="self_unenrollment_dialog" style="display: none;">
        <h2><i class="icon-warning"></i> {{#t}}Confirm Unenrollment{{/t}}</h2>
        {{#t}}Are you sure you want to unenroll in this course?  You will no longer be able to see the course roster or communicate directly with the teachers, and you will no longer see course events in your stream and as notifications.{{/t}}
        <div class="button-container">
          <a href="<%= course_self_unenrollment_path(@context, @context_enrollment.uuid) %>" class="btn btn-primary action"><i class="icon-end"></i> <span>{{#t}}Drop this Course{{/t}}</span></a>
          <a href="#" class="btn dialog_closer">{{#t}}Cancel{{/t}}</a>
        </div>
      </div>
    <% end %>
    {{> views/to_do_list}}
    {{> views/group_list}}
    <%= nbsp unless @current_user %>
  </div>
  <% if @course_home_view == 'assignments' %>
    {{> views/assignments/assignments_list_right_side}}
  <% elsif @course_home_view == 'syllabus' %>
    {{> views/assignments/syllabus_right_side}}
  <% else %>
    <% locals = {:title => t("Coming Up"), :contexts_to_link_to => @context, :upcoming => true, :period => :one_week} %>
    <% if @current_user %>
      <% cache(safe_cache_key([@current_user, @contexts, 'course_upcoming_events' ])) do %>
        {{> views/shared/event_list}}
      <% end %>
    <% else %>
      {{> views/shared/event_list}}
    <% end %>
  <% end %>
  <% if @show_recent_feedback %>
    {{> views/shared/event_list", :object => @recent_feedback, :locals => {:title => t('titles.recent_feedback', "Recent Feedback}}
  <% end %>
<% end %>

<%
  css_bundle :dashboard
  case @course_home_view
  when 'wiki'
    css_bundle :tinymce
  when 'modules'
    js_bundle :context_modules
    css_bundle :context_modules2
  when 'assignments'
    js_bundle :assignment_index
    css_bundle :new_assignments
  when 'syllabus'
    js_bundle :syllabus
    css_bundle :tinymce
  else
    js_bundle :dashboard
  end
%>

<% js_bundle 'legacy/courses_show' %>
<% if @pending_enrollment %>
  {{> views/shared/dashboard_invitation}}
<% end %>

<% if @context.show_announcements_on_home_page? %>
  <div id="announcements_on_home_page"></div>
  <% js_bundle :announcements_on_home_page %>
<% end %>
<div id="course_home_content">
  <% if @course_home_view == 'wiki' %>
    <% js_bundle :wiki_page_show %>
    <div id="wiki_page_show"></div>
  <% elsif @course_home_view == 'modules' %>
    {{> views/context_modules/content_next}}
  <% elsif @course_home_view == 'assignments' %>
    <%
      @body_classes << 'hide-content-while-scripts-not-loaded'
      @body_classes << 'with_item_groups'
      js_bundle :assignment_index
      css_bundle :new_assignments
    %>
  <% elsif @course_home_view == 'syllabus' %>
    {{> views/assignments/syllabus_content}}
  <% else %>
    {{> views/shared/dashboard_messages}}
    {{> views/shared/recent_activity}}
  <% end %>
</div>
<% if @context.public_license? %>
  <div class="public-license" style="margin-top: 20px;">

    <a class="public-license-image-link" href="<%= @context.license_url %>"><%= image_tag "cc/#{@context.license}.png", :alt => @context.readable_license   %></a>
    <span class="public-license-text"><%= t 'license_notice', %{This course content is offered under a %{license_type} license.  Content in this course can be considered under this license unless otherwise noted.}, :license_type => content_tag('b', link_to(@context.readable_license, @context.license_url)) %></span>
    <div class="clear"></div>
  </div>
<% end %>
<div id="wizard_box"></div>

<% js_bundle :course %>
